package com.example.yadmin.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yadmin.common.vo.Result;
import com.example.yadmin.sys.entity.Goods;
import com.example.yadmin.sys.entity.User;
import com.example.yadmin.sys.mapper.GoodsMapper;
import com.example.yadmin.sys.service.IGoodsService;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ag
 * @since 2023-10-13
 */
@Api(tags = {"物品接口列表"})
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private GoodsMapper goodsMapper;
    //获取所有物品列表
    @GetMapping("/all")
    public Result<List<Goods>> getAllGoods(){
        List<Goods> list = goodsService.list();
        return Result.success(list,"查询成功");
    }

    //查询物品列表
    @GetMapping("/list")
    public Result<Map<String,Object>> getGoodsList(@RequestParam(value = "goodsName",required = false) String goodsName,
                                                   @RequestParam(value = "goodsOwner",required = false) String goodsOwner,
                                                   @RequestParam(value = "pageNo") Long pageNo,
                                                   @RequestParam(value = "pageSize") Long pageSize){
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasLength(goodsName),Goods::getGoodsName,goodsName);
        wrapper.like(StringUtils.hasLength(goodsOwner),Goods::getGoodsOwner,goodsOwner);
        wrapper.orderByDesc(Goods::getGoodsId);
        Page<Goods> page = new Page<>(pageNo, pageSize);
        goodsService.page(page,wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getRecords());

        return Result.success(data);
    }

    //增
    @PostMapping
    public Result<?> addGoods(@RequestBody Goods goods){
        goodsService.addGoods(goods);
        return Result.success("新增订单成功");
    }
    //删
    @DeleteMapping("/{id}")
    public Result<?> deleteGoodsById(@PathVariable("id") Integer id){
        goodsService.deleteGoodsById(id);
        return Result.success("删除订单成功");
    }

    //改
    @PutMapping
    public Result<?> updateGoods(@RequestBody Goods goods){
        goodsService.updateGoods(goods);
        return Result.success("修改订单成功");
    }
    //查
    @GetMapping("/{id}")
    public Result<Goods> getGoodsById(@PathVariable("id")Integer id){
        Goods goods = goodsService.getGoodsById(id);
        return Result.success(goods);
    }

    //根据物品所属者查找订单
    @GetMapping("/info")
        public Result<List<Goods>> getGoodsInfoByUsername(@RequestParam(value = "username") String username){
       List<Goods> list = goodsService.getGoodsInfoByUsername(username);
       return Result.success(list);
    }

    //根据物品所属者查找用户列表
    @GetMapping("/owner")
        public Result<List<User>> getUserListInfoByOwner(@RequestParam(value = "goodsOwner") String goodsOwner){
        List<User> list = goodsService.getUserListInfoByOwner(goodsOwner);
        return Result.success(list);
    }

//    @GetMapping
//        public Result<List<Goods>> getGoodsByGoodsName(@RequestParam(value = "goodsName") String goodsName){
//        List<Goods> list = goodsMapper.getGoodsByGoodsName(goodsName);
//        return Result.success(list);
//    }

    @GetMapping
    public Result<List<Goods>> getGoodsBySelect(@RequestParam(value = "goodsName",required = false) String goodsName,
                                                @RequestParam(value = "goodsOwner",required = false) String goodsOwner,
                                                @RequestParam(value = "goodsDesc",required = false) String goodsDesc){
        List<Goods> list = goodsMapper.getGoodsBySelect(goodsName,goodsOwner,goodsDesc);
        return Result.success(list);
    }

    @GetMapping("/price")
    public Result<List<Goods>> getGoodsByPrice(@RequestParam(value = "minPrice",required = false)BigDecimal minPrice,
                                               @RequestParam(value = "maxPrice",required = false)BigDecimal maxPrice){
        List<Goods> list = goodsMapper.getGoodsByPrice(minPrice, maxPrice);
        return Result.success(list);
    }

    @GetMapping("/getId")
    public Result<Integer> getGoodsIdByGoodsName(@RequestParam(value = "goodsName")String goodsName){
        Integer goodsId = goodsMapper.getGoodsIdByGoodsName(goodsName);
        return Result.success(goodsId);
    }
}
